<?php


namespace King\Service;


class PokerRuleService
{
    public $type;
    public $len = 0;
    public $numList = [];
    public $valueCountList;

    public function make($pokerList): PokerRuleService
    {
        $numberList = [];
        foreach ($pokerList as $index => $item) {
            array_push($numberList, $item->name);
        }
        $this->numList = $numberList;
        $this->len = count($this->numList);
        return $this->rulesCheck();
    }

    public function rulesCheck(): PokerRuleService
    {
        if ($this->len < 2) {
            $this->singleCard();
        }
        if (in_array('trump', $this->numList) && in_array('joker', $this->numList)) {
            $this->kingBelt();
        } else {
            $this->pair();
            $this->leopard();
            $this->bomb();
        }
        if (empty($this->type)) {
            $this->straight();
            $this->kingBomb();
        }
        if (!$this->type) {
            throw new \Exception('error rule');
        }
        return $this;
    }


    public function straight(): bool
    {
        if ($this->len > 4) {
            sort($this->numList);
            $head = (new \ArrayObject($this->numList))->offsetGet(0);
            $pos = array_search($head, PokerService::$pokerList);
            $sliceList = array_slice(PokerService::$pokerList, $pos, $this->len);
            $compareList = array_intersect($sliceList, $this->numList);
            if (count($compareList) == $this->len) {
                $this->type = __FUNCTION__;
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public function pair(): bool
    {
        $len = $this->len;
        $list = [2, 6, 8, 10, 12, 14, 16, 18, 20];
        if (in_array($len, $list)) {
            $countList = array_count_values($this->numList);
            $this->valueCountList = $countList;
            if (in_array(2, $countList) && !in_array(4, $countList)) {
                $this->type = __FUNCTION__;
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public function leopard(): bool
    {
        $len = $this->len;
        $list = [3, 6, 9, 4, 5, 8, 10, 12, 15];
        if (in_array($len, $list)) {
            $countList = array_count_values($this->numList);
            $this->valueCountList = $countList;
            if (in_array(3, $countList)) {
                $this->type = __FUNCTION__;
                return true;
            } else {
                return false;
            }
        }
        return false;
    }

    public function bomb(): bool
    {
        $len = $this->len;
        $list = [4, 6, 8, 12, 16];
        if (in_array($len, $list)) {
            $countList = array_count_values($this->numList);
            $this->valueCountList = $countList;
            if (in_array(4, $countList)) {
                $this->type = __FUNCTION__;
                return true;
            } else {
                return false;
            }
        }

        return false;
    }

    public function kingBomb(): bool
    {
        if (
            $this->len == 2 &&
            (
                in_array('joker', $this->numList) ||
                in_array('trump', $this->numList))
        ) {
            $this->type = __FUNCTION__;
            return true;
        } else {
            return false;
        }
    }

    public function kingBelt(): bool
    {
        $len = $this->len;
        $list = [4, 6];
        if (in_array($len, $list)) {
            $countList = array_count_values($this->numList);
            $countList = array_count_values($countList);
            $this->valueCountList = $countList;
            if (in_array(2, $countList)) {
                $this->type = __FUNCTION__;
                return true;
            } else {
                if ($len == 4) {
                    $this->type = __FUNCTION__;
                    return true;
                }
            }
        }
        return false;
    }

    public function singleCard(): bool
    {
        if ($this->len == 1) {
            $this->type = __FUNCTION__;
            return true;
        } else {
            return false;
        }
    }


}